package cn.edu.hqu.cst.android.sensor;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SensorSimulation extends AppCompatActivity implements SensorEventListener{
    TextView direction_show;
    TextView magnetic_show;
    TextView temp_show;
    TextView light_show;
    TextView pressure_show;

    private SensorManager sensor_direction;
    private SensorManager sensor_magnetic;
    private SensorManager sensor_temp;
    private SensorManager sensor_light;
    private SensorManager sensor_pressure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor_simulation);
        //设置返回
        Button back = findViewById(R.id.backpage_sen);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(SensorSimulation.this,MainActivity.class);
                startActivity(i);
            }
        });
        //信息显示文本框
        direction_show = findViewById(R.id.direction_show);
        magnetic_show = findViewById(R.id.magnetic_show);
        temp_show = findViewById(R.id.temp_show);
        light_show = findViewById(R.id.light_show);
        pressure_show = findViewById(R.id.pressure_show);
        //--------------------------------------------------------------------------
        //传感器类型设置
        //方向传感器
        sensor_direction = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor_direction.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        //磁场传感器
        sensor_magnetic = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor_magnetic.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        //温度传感器
        sensor_temp = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor_temp.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
        //光线传感器
        sensor_light = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor_light.getDefaultSensor(Sensor.TYPE_LIGHT );
        //压力传感器
        sensor_pressure = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor_pressure.getDefaultSensor(Sensor.TYPE_PRESSURE );
        //------------------------------------------------------------------------------------
    }
    //方向传感器模块
    protected void onResume(){
        super.onResume();// 为系统的传感器注册监听器
        sensor_direction.registerListener((SensorEventListener) this,
                sensor_direction.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
        sensor_magnetic.registerListener((SensorEventListener) this,
                sensor_direction.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_GAME);
        sensor_temp.registerListener((SensorEventListener) this,
                sensor_direction.getDefaultSensor(Sensor.TYPE_TEMPERATURE),
                SensorManager.SENSOR_DELAY_GAME);
        sensor_light.registerListener((SensorEventListener) this,
                sensor_direction.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_GAME);
        sensor_pressure.registerListener((SensorEventListener) this,
                sensor_direction.getDefaultSensor(Sensor.TYPE_PRESSURE),
                SensorManager.SENSOR_DELAY_GAME);
    }
    protected void onStop(){
        // 取消注册
        sensor_direction.unregisterListener((SensorEventListener) this);
        sensor_magnetic.unregisterListener((SensorEventListener) this);
        sensor_temp.unregisterListener((SensorEventListener) this);
        sensor_light.unregisterListener((SensorEventListener) this);
        sensor_pressure.unregisterListener((SensorEventListener) this);
        super.onStop();
    }

    // 以下是实现SensorEventListener接口必须实现的方法
    // 当传感器的值发生改变时回调该方法
    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] values = event.values;
        //获取传感器类型
        int type = event.sensor.getType();
        StringBuilder sb;
        switch(type){
            //方向传感器
            case Sensor.TYPE_ORIENTATION :
                sb = new StringBuilder();
                sb.append("绕Z轴转过的角度：");
                sb.append(values[0]);
                sb.append("\n绕X轴转过的角度：");
                sb.append(values[1]);
                sb.append("\n绕Y轴转过的角度：");
                sb.append(values[2]);
                direction_show.setText(sb.toString());
                break;
            //磁场传感器
            case Sensor.TYPE_MAGNETIC_FIELD :
                sb = new StringBuilder();
                sb.append("X轴方向上的磁场强度：");
                sb.append(values[0]);
                sb.append("\nY轴方向上的磁场强度：");
                sb.append(values[1]);
                sb.append("\nZ轴方向上的磁场强度：");
                sb.append(values[2]);
                magnetic_show.setText(sb.toString());
                break;
            //温度传感器
            case Sensor.TYPE_TEMPERATURE :
                sb = new StringBuilder();
                sb.append("当前温度为：");
                sb.append(values[0]);
                temp_show.setText(sb.toString());
                break;
            //光线传感器
            case Sensor.TYPE_LIGHT :
                sb = new StringBuilder();
                sb.append("当前光的强度为：");
                sb.append(values[0]);
                light_show.setText(sb.toString());
                break;
            //压力传感器
            case Sensor.TYPE_PRESSURE :
                sb = new StringBuilder();
                sb.append("当前压力为：");
                sb.append(values[0]);
                pressure_show.setText(sb.toString());
                break;
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
}